package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.FlowCardDao;
import com.framework.entity.flow.FlowCard;
import com.framework.entity.sys.SysAgent;
import com.framework.service.AgentService;
import com.framework.service.FlowCardService;
import com.framework.utils.string.StringUtil;
import com.framework.view.FlowCardInfoView;
import com.framework.view.SeparatMonthView;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("flowCardService")
public class FlowCardServiceImpl extends ServiceImpl<FlowCardDao, FlowCard> implements FlowCardService {

	@Autowired
	private AgentService agentService;


	@Override
	public BigDecimal getThisMonthAmount(Long agentId) {
		return baseMapper.getThisMonthAmount(agentId);
	}

	@Override
	public List<SeparatMonthView> flowCardInfoList(String token) {
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		//查询半年时间范围值
		List<SeparatMonthView> monthList = StringUtil.getLastMonthsList(6);
		for (int i = 0; i < monthList.size(); i++) {
			String monthTime = monthList.get(i).getTime();
			BigDecimal monthAmount = baseMapper.queryMonthAmount(agentId,monthTime);
			monthList.get(i).setAmount(monthAmount);
			//查询一个月中有数据的天数
			List<Map<String,Object>> mapList = new ArrayList<>();
			List<String> dayList = baseMapper.queryRecDayList(agentId,monthTime);
			for (int j = 0; j < dayList.size(); j++) {
				Map<String,Object> map = new HashedMap();
				String dayTime = dayList.get(j);
				map.put("dayTime",dayTime);
				List<FlowCardInfoView> dayListInfo = baseMapper.queryRecDayInfoList(agentId,dayTime);
				map.put("dayListInfo",dayListInfo);
				mapList.add(map);
				monthList.get(i).setObjList(mapList);
			}
		}

		return monthList;
	}
}
